package zilvic.game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Keeps track of wich buttons is currently down.
 * @author Duncan
 */
public class KeyInputHandler extends KeyAdapter {
    public final static int KEY_MOVE_UP     = 0;
    public final static int KEY_MOVE_LEFT   = 1;
    public final static int KEY_MOVE_DOWN   = 2;
    public final static int KEY_MOVE_RIGHT  = 3;
    public final static int KEY_MENU        = 4;
    public final static int KEY_FIRST_HAND  = 5; // Click, select, attack etc
    public final static int KEY_SECOND_HAND = 6; // Shieldblock, interact

    private int[] keyCode = {
        KeyEvent.VK_W,
        KeyEvent.VK_A,
        KeyEvent.VK_S,
        KeyEvent.VK_D,
        KeyEvent.VK_ESCAPE,
        KeyEvent.VK_K,
        KeyEvent.VK_L
    };

    private boolean[] keyDown = {
        false,
        false,
        false,
        false,
        false,
        false,
        false
    };

    @Override
    public void keyPressed(KeyEvent e) {
        for (int i = 0; i < keyCode.length; i++)
            if (e.getKeyCode() == keyCode[i])
                keyDown[i] = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        for (int i = 0; i < keyCode.length; i++)
            if (e.getKeyCode() == keyCode[i])
                keyDown[i] = false;
    }

    public boolean isKeyDown(int key) {
        return keyDown[key];
    }

    public void setKeyDown(int key, boolean isDown) {
        keyDown[key] = isDown;
    }
}
